package net.bolue.sysmng.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.PropertyResourceBundle;

public class PropertiesLoader {

	public static PropertyResourceBundle getConfigBundle(String properties) {
		PropertyResourceBundle configBundle = (PropertyResourceBundle) PropertyResourceBundle
				.getBundle(properties);
		if (configBundle == null) {
			System.out.println("文件" + properties + ".properties读入错误");
		}
		return configBundle;
	}
	
	/**
	 * 得到资源文件的值对
	 * @param properties
	 * @param key
	 * @return
	 */
	public static String getKeyValue(String properties, String key) {
		PropertyResourceBundle configBundle = PropertiesLoader
				.getConfigBundle(properties);
		return configBundle.getString(key);
	}
	
	static public Properties loadPropertiesResource(String aResourcePath) throws IOException {
		try {
			// Use the class loader that loaded our class.
			// This is required where for reasons like security
			// multiple class loaders exist, e.g. BEA WebLogic.
			// Thanks to Lutz Lennemann 29-aug-2000.
			ClassLoader classLoader = PropertiesLoader.class.getClassLoader();

			Properties properties = new Properties();

			// Try loading it.
			properties.load(classLoader.getResourceAsStream(aResourcePath));
			return properties;
		} catch (Throwable t) {
			throw new IOException("failed loading Properties resource from " + aResourcePath);
		}
	}

	/**
	 * Load properties file from file path.
	 * 直接从文件中加载一个属性文件
	 */
	static public Properties loadPropertiesFile(String aFilePath) throws IOException {
		try {

			Properties properties = new Properties();

			// Try loading it.
			properties.load(new FileInputStream(aFilePath));
			return properties;
		} catch (Throwable t) {
			throw new IOException("failed loading Properties file from " + aFilePath);
		}
	}
}
